﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    public class PersonnelGroupPermissionViewModel : NotificationObject
    {
        private PersonnelGroupPermission gWindow;
        private LPersonnelGroup gLPersonnelGroup;

        public PersonnelGroupPermissionViewModel(PersonnelGroupPermission pWindow, LPersonnelGroup pLPersonnelGroup)
        {
            gWindow = pWindow;
            gLPersonnelGroup = pLPersonnelGroup;

            Initialize();
        }

        public void Initialize()
        {
            //初始化用户组名
            List<WPersonnelGroup> personnelGroupList = WPersonnelGroup.GetAll();
            for (int i = 0;i<personnelGroupList.Count; i++)
            {
                PersonnelGroups.Add(personnelGroupList[i].PersonnelGroupName.Trim());
                if (personnelGroupList[i].PersonnelGroupName.Trim() == gLPersonnelGroup.PersonnelGroupName.Trim())
                {
                    PersonnelGroupsSelectedIndex = i;
                }
            }

            //获取当前用户名下的权限列表
            List<WPersonnelGroupPermission> wPersonnelGroupPermissionList = WPersonnelGroupPermission.Select(gLPersonnelGroup.PersonnelGroupName.Trim());

            //初始化用户组权限
            List<WPermission> list = WPermission.GetAll();
            for (int i = 0; i < list.Count; i++)
            {
                CheckBox checkBox = new CheckBox();
                checkBox.Margin = new System.Windows.Thickness(5);
                checkBox.Content = list[i].PermissionName.Trim();
                gWindow.PersonnelGroupPermissions.Children.Add(checkBox);

                foreach(var item in wPersonnelGroupPermissionList)
                {
                    if(item.PermissionUID.Trim() == list[i].PermissionUID.Trim())
                    {
                        checkBox.IsChecked = true;
                        break;
                    }
                }
            }
        }

        private List<String> personnelGroups = new List<String>();
        public List<String> PersonnelGroups
        {
            get
            {
                return personnelGroups;
            }

            set
            {
                personnelGroups = value;
                RaisePropertyChanged("PersonnelGroups");
            }
        }

        private int personnelGroupsSelectedIndex;
        public int PersonnelGroupsSelectedIndex
        {
            get
            {
                return personnelGroupsSelectedIndex;
            }

            set
            {
                personnelGroupsSelectedIndex = value;
                RaisePropertyChanged("PersonnelGroupsSelectedIndex");
            }
        }

        protected DelegateCommand<object> personnelGroupSelectionChangedCommand;
        public DelegateCommand<object> PersonnelGroupSelectionChangedCommand
        {
            get
            {
                return personnelGroupSelectionChangedCommand ?? (personnelGroupSelectionChangedCommand = new DelegateCommand<object>(CanPersonnelGroupSelectionChangedCommand));
            }
        }

        protected virtual void CanPersonnelGroupSelectionChangedCommand(object obj)
        {
            List<WPermission> wPermission = WPermission.GetAll();
            List<WPersonnelGroupPermission> wPersonnelGroupPermissionList = WPersonnelGroupPermission.Select(PersonnelGroups[PersonnelGroupsSelectedIndex]);

            List<String> list = new List<string>();
            foreach (var personnelGroupPermission in wPersonnelGroupPermissionList)
            {
                foreach (var permission in wPermission)
                {
                    if(permission.PermissionUID.Trim() == personnelGroupPermission.PermissionUID.Trim())
                    {
                        list.Add(permission.PermissionName.Trim());
                        break;
                    }
                }
            }

            for (int i = 0; i < gWindow.PersonnelGroupPermissions.Children.Count; i++)
            {
                if (gWindow.PersonnelGroupPermissions.Children[i] is CheckBox)
                {
                    ((CheckBox)gWindow.PersonnelGroupPermissions.Children[i]).IsChecked = false;
                }
            }

            for (int i = 0; i< gWindow.PersonnelGroupPermissions.Children.Count; i++)
            {
                if(gWindow.PersonnelGroupPermissions.Children[i] is CheckBox)
                {
                    String content = (String)((CheckBox)gWindow.PersonnelGroupPermissions.Children[i]).Content;

                    foreach(var item in list)
                    {
                        if(item == content)
                        {
                            ((CheckBox)gWindow.PersonnelGroupPermissions.Children[i]).IsChecked = true;
                        }
                    }
                }
            }
        }

        protected DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                return saveCommand ?? (saveCommand = new DelegateCommand<object>(CanSaveCommand));
            }
        }

        protected virtual void CanSaveCommand(object obj)
        {
            int personnelGroupUID = WPersonnelGroup.GetPersonnelGroupUID(PersonnelGroups[PersonnelGroupsSelectedIndex]);
            List<WPermission> wPermissionList = WPermission.GetAll();

            //先删除相关的权限
            WPersonnelGroupPermission.Delete(personnelGroupUID);

            //添加权限
            List<WPersonnelGroupPermission> wPersonnelGroupPermissionList = new List<WPersonnelGroupPermission>();
            for (int i = 0; i < gWindow.PersonnelGroupPermissions.Children.Count; i++)
            {
                if (gWindow.PersonnelGroupPermissions.Children[i] is CheckBox)
                {
                    if(((CheckBox)gWindow.PersonnelGroupPermissions.Children[i]).IsChecked == true)
                    {
                        foreach(var item in wPermissionList)
                        {
                            if(item.PermissionName.Trim() == ((String)((CheckBox)gWindow.PersonnelGroupPermissions.Children[i]).Content).Trim())
                            {
                                WPersonnelGroupPermission wPersonnelGroupPermission = new WPersonnelGroupPermission();
                                wPersonnelGroupPermission.PermissionUID = item.PermissionUID;
                                wPersonnelGroupPermission.PersonnelGroupUID = personnelGroupUID;
                                wPersonnelGroupPermissionList.Add(wPersonnelGroupPermission);
                                break;
                            }
                        }
                    }
                }
            }
            WPersonnelGroupPermission.Insert(wPersonnelGroupPermissionList);

            gWindow.Close();
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(CanQuitCommand));
            }
        }

        protected virtual void CanQuitCommand(object obj)
        {
            gWindow.Close();
        }
    }
}
